package com.ssh;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

public class GroupAnagrams_49 {
	public static void main(String[] args) {
		
		/*Map<String, Integer> map1 = new HashMap<String, Integer>();
		
		Map<String, Integer> map2 = new HashMap<String, Integer>();
		
		map1.put("s", 1);
		map1.put("a", 2);
		map1.put("d", 1);
		
		map2.put("s", 1);
		map2.put("a", 2);
		map2.put("d", 1);
		
		System.out.println(map1.equals(map2));*/
		
		String[] s = {"tea","fer","aet","ss","fff","ff","aass","dd","ff"};
		System.out.println(groupAnagrams(s));
		
	}
	public static List<List<String>> groupAnagrams(String[] strs) {
		if (strs.length == 0 || strs == null) {
			return new ArrayList<List<String>>();
		}
		Map<String, List<String>> map = new HashMap<String, List<String>>();
		for (String s : strs) {
			char[] ch = s.toCharArray();
			Arrays.sort(ch);
			String str = String.valueOf(ch);
			if (!map.containsKey(str)) {
				map.put(str, new ArrayList<String>());
			}
			map.get(str).add(s);
		}
		return new ArrayList<List<String>>(map.values());
		
		
	}
}
